<template>
  <el-container v-loading="loading">
    <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="180px" label-position="left" size="small">

      <el-form-item label="接单成功提醒模板" prop="receipt_tpl">
        <el-input v-model="ruleForm.receipt_tpl"></el-input>
        <div class="form-item-tip">
          <span>提示：*模板编号AT0328,关键词(订单号,订单状态,接单时间,接单人,联系电话,提单号,服务类型)</span>
        </div>
      </el-form-item>

      <el-form-item label="订单支付成功通知模板" prop="payment_tpl">
        <el-input v-model="ruleForm.payment_tpl"></el-input>
        <div class="form-item-tip">
          <span>提示：*模板编号AT0009,关键词(商品名称、订单状态、订单编号、支付金额)</span>
        </div>
      </el-form-item>

      <el-form-item label="退款成功通知模板" prop="refund_tpl">
        <el-input v-model="ruleForm.refund_tpl"></el-input>
        <div class="form-item-tip">
          <span>提示：*模板编号AT0787,关键词(商品名称、订单号、退款金额、退款时间)</span>
        </div>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" @click="submitForm('ruleForm')">立即保存</el-button>
        <!--<el-button @click="resetForm('ruleForm')">重置</el-button>-->
      </el-form-item>
    </el-form>

  </el-container>
</template>

<script>
  export default {
    data() {
      return {
        loading: true,  // 页面加载
        ruleForm: {
          "payment_tpl":"",                //类型：String  必有字段  备注：支付模板消息
          "receipt_tpl":"",                //类型：String  必有字段  备注：接单模板消息
          "refund_tpl":""                //类型：String  必有字段  备注：退款模板消息
        },
        rules: {
          payment_tpl: [
            {required: true, message: '订单支付成功通知模板必须配置', trigger: 'blur'},
          ],
          receipt_tpl: [
            {required: true, message: '接单成功提醒模板必须配置', trigger: 'blur'},
          ],
          refund_tpl: [
            {required: true, message: '退款成功通知模板必须配置', trigger: 'blur'},
          ],
        },
      };
    },
    methods: {
      submitForm(formName) {
        let _this = this;
        this.$refs[formName].validate((valid) => {
          if (valid) {
            _this.api.updateTempletMessageConfig(_this.ruleForm).then(function (res) {
              if (res.code == 1) {
                _this.$message({
                  showClose: true,
                  message: res.msg,
                  type: 'success'
                });
              } else {
                _this.$message({
                  showClose: true,
                  message: res.msg,
                  type: 'warning'
                });
              }
            }).catch(function (error) {
              throw new Error(error);
            });
          } else {
            console.log('error submit!!');
            return false;
          }
        });
      },
      resetForm(formName) {
        this.$refs[formName].resetFields();
      }
    },

    mounted: function () {
      let _this = this;
      this.api.getTempletMessageConfig().then(function (res) {
        if (res.code == 1) {
          _this.ruleForm = res.data;
        }
        _this.loading = false;
      }).catch(function (res) {
        throw new Error(error);
      });
    },
  }
</script>

<style scoped>

  .el-container {
    display: block;
  }

  /* 表单 */
  .el-form {
    width: 1000px;
    margin: 15px 0 15px 30px;
  }

  /* 图片上传 */
  .avatar-uploader {
    border: 1px dashed #d9d9d9;
    width: 178px;
  }

  .avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }

  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }

  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
  }

  .avatar {
    width: 178px;
    height: 178px;
    display: block;
  }

</style>
